/**
 * 从尾到头打印链表
 * @param head 
 */
const reversePrint = (head: ListNode | null): number[] => {
    // 链表为空
    if (head === null) return []

    // 翻转链表
    let newHead = null
    while (head !== null) {
        const n: ListNode | null = head.next;
        head.next = newHead;
        newHead = head;
        head = n
    }


    const arr = []
    // 遍历链表, 从头插入
    while (newHead !== null) {
        arr.push(newHead.val)
        newHead = newHead.next
    }

    return arr
};


const reversePrint3 = (head: ListNode | null): number[] => {
    // 链表为空
    if (head === null) return []

    const arr = []
    // 遍历链表, 从头插入
    while (head !== null) {
        arr.push(head.val)
        head = head.next
    }

    return arr.reverse()
};

const reversePrint2 = (head: ListNode | null): number[] => {
    // 链表为空
    if (head === null) return []
    const arr: number[] = []

    // 遍历链表, 从头插入
    while (head !== null) {
        arr.unshift(head.val)
        head = head.next
    }

    return arr
};